<template>
    <div>
        <div class="job">
            <span style="color:red;">带*是必填项</span>
            <el-form :model="jobForm" label-width="80px" size="medium" style="margin-left:300px;margin-top:10px">
                <el-form-item label="职位名称*" style="width:600px">
                    <el-input v-model="jobForm.job_title"></el-input>
                </el-form-item>
                <el-form-item label="职位分类*" style="width:600px">
                    <el-select v-model="jobForm.job_classification" placeholder="职位分类">
                        <el-option v-for="job_classification in job_classification_list"
                            :key="job_classification.job_classification_id" :value="job_classification.job_classification"
                            :label="job_classification.job_classification"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="薪资*" style="width:600px">
                    <el-input v-model="jobForm.salary"></el-input>
                </el-form-item>
                <el-form-item label="职位要求*" style="width:600px;">
                    <el-input v-model="jobForm.job_requirements" type="textarea"
                        :autosize="{ minRows: 5, maxRows: 40 }"></el-input>
                </el-form-item>
                <el-form-item label="职位待遇*" style="width:600px">
                    <el-input v-model="jobForm.position_treatment" type="textarea"
                        :autosize="{ minRows: 5, maxRows: 40 }"></el-input>
                </el-form-item>
                <el-form-item label="公司简介*" style="width:600px">
                    <el-input v-model="jobForm.company_profile" type="textarea" :autosize="{ minRows: 5, maxRows: 40 }">
                    </el-input>
                </el-form-item>
                <el-button type="primary" @click="publishJob(jobForm)" style="margin-left:-100px;">提交</el-button>
            </el-form>
        </div>
    </div>
</template>
<script>
import axios from 'axios';
export default {
    data() {
        return {
            job_classification_list: [],
            jobForm: {
                job_title: '',
                job_classification: '',
                enterprise_name: '',
                job_requirements: '',
                salary: '',
                cover: '',
                enterprise_number: '',
                position_treatment: '',
                company_profile: '',
            }
        };
    },
    methods: {
        publishJob(jobForm) {
            if (!this.jobForm.job_title) {
                this.$message.error("请输入职位名称!");
                return false;
            }
            if (!this.jobForm.job_classification) {
                this.$message.error("请选择职位分类!");
                return false;
            }
            if (!this.jobForm.salary) {
                this.$message.error("请输入薪资!");
                return false;
            }
            if (!this.jobForm.job_requirements) {
                this.$message.error("请输入职位要求!");
                return false;
            }
            if (!this.jobForm.position_treatment) {
                this.$message.error("请输入职位待遇!");
                return false;
            }
            if (!this.jobForm.company_profile) {
                this.$message.error("请输入公司简介!");
                return false;
            }
            let that = this;
            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/api/job_recruitment/add',
                data: {
                    job_title: jobForm.job_title,
                    job_classification: jobForm.job_classification,
                    enterprise_name: jobForm.enterprise_name,
                    job_requirements: jobForm.job_requirements,
                    salary: jobForm.salary,
                    cover: jobForm.cover,
                    enterprise_number: jobForm.enterprise_number,
                    position_treatment: jobForm.position_treatment,
                    company_profile: jobForm.company_profile,
                },
                headers: {
                    'X-Auth-Token': window.localStorage.getItem("token")
                },
            })
                .then(function (response) {
                    console.log(response.data);
                    that.$message.success("发布职位成功");
                    that.$router.push({ name: 'enterprise_job_list' }, () => { }, () => { });
                })
                .catch(function (error) {
                    console.log(error);
                })
                .then(function () {
                    // 总是会执行
                })
        },
    },
    beforeMount: function () {
        let that = this;
        axios({
            method: 'get',
            url: 'http://127.0.0.1:5000/api/job_classification/get_list',
            params: {

            },
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
        })
            .then(function (response) {
                console.log(response.data);
                that.job_classification_list = response.data.result.list;
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    },
    created: function () {
        let that = this;
        axios({
            method: 'get',
            url: 'http://127.0.0.1:5000/api/school_recruitment_enterprise/get_obj',
            params: {
                user_id: window.localStorage.getItem("user_id")
            },
            headers: {
                'X-Auth-Token': window.localStorage.getItem("token")
            },
        })
            .then(function (response) {
                console.log(response.data);
                if (!response.data.result) {
                    that.$message.error("您还没有完善企业信息!请去完善企业信息!!!");
                    that.$router.push({ name: 'enterprise_information' }, () => { }, () => { });
                }
                that.jobForm.enterprise_name = response.data.result.obj.enterprise_name;
                that.jobForm.enterprise_number = response.data.result.obj.enterprise_number;
            })
            .catch(function (error) {
                console.log(error);
            })
            .then(function () {
                // 总是会执行
            })
    }
};
</script>
<style lang="less" scoped>
.job {
    margin-top: -450px;
    margin-left: 500px;
}
</style>